﻿using BLL;
using DBUtility;
using gfLibrary;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IYAKASAPortal
{
    public partial class SYS_USER : System.Web.UI.Page
    {
        public string g_strTreeJS = "";

        public static string str_select_value { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            str_select_value = "";
            g_strTreeJS = BuildTree(str_select_value);

            if (Page.IsPostBack == false)
            {
                BindData();
            }
        }

        protected void BindData()
        {
            CONST_DEFINE_BLL bll = new CONST_DEFINE_BLL();
            var role = bll.GetModelList("CON_CHNAME='ZNYHGW'");
            ListItem item;
            foreach (var r in role)
            {
                item = new ListItem();
                item.Text = r.CON_CON;
                item.Value = r.CON_CODE;
                rbtnRole.Items.Add(item);  
            }
        }

        public string BuildTree(string p_strSelectedNodeValue)
        {
            string m_strTreeJS = "";

            SYSTEM_MODULE_BLL m_Modules_BLL = new SYSTEM_MODULE_BLL();
            SYSTEM_MODULE[] m_arrNodes = m_Modules_BLL.GetModelList("", "morder asc");


            string strPrefix = "";
            string strSuffix = "";

            m_strTreeJS = "d.add(0,-1,'弥荣登录系统','javascript:;','<input type=\"checkbox\" id=\"rn\" name=\"rn\" value=\"0\">');";
            if (p_strSelectedNodeValue.IndexOf(",0,")>=0)
            {
                m_strTreeJS = "d.add(0,-1,'弥荣登录系统','javascript:;','<input type=\"checkbox\" id=\"rn\" name=\"rn\" value=\"0\" checked>');";
            }

            foreach (SYSTEM_MODULE m_Node in m_arrNodes)
            {
                strPrefix = "<input type=\"checkbox\" id=\"rn\" name=\"rn\" value=\"" + m_Node.mid.ToString() + "\">";
                //if (m_Node.mid.ToString().Equals(p_strSelectedNodeValue) == true)
                if(p_strSelectedNodeValue.IndexOf(","+m_Node.mid.ToString()+",")>=0)
                {
                    strPrefix = "<input type=\"checkbox\" id=\"rn\" name=\"rn\" value=\"" + m_Node.mid.ToString() + "\" checked>";
                }

                strSuffix = "&nbsp;&nbsp;";


                string strQyStatus = m_Node.mdisabled.Equals("1") == true ? "启用" : "禁用";
                if (m_Node.murl.Trim().Length == 0)
                {
                    m_strTreeJS += "d.add(" + m_Node.mid.ToString() + "," + m_Node.pid.ToString() + ",'" + "[" + strQyStatus + "]&nbsp;" + m_Node.mname + "&nbsp;[" + m_Node.morder.ToString() + "]" + "','javascript:;','" + strPrefix + "');";
                }
                else
                {
                    m_strTreeJS += "d.add(" + m_Node.mid.ToString() + "," + m_Node.pid.ToString() + ",'" + "[" + strQyStatus + "]&nbsp;" + m_Node.mname + "&nbsp;[" + m_Node.morder.ToString() + "]" + "','javascript:;','" + strPrefix + "','" + strSuffix + "');";
                }
            }

            //m_dtControls.Dispose();

            return m_strTreeJS;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strRole = "";
            string strRights = "";

            DbFactory m_df = new DbFactory();

            SYSTEM_ROLE_RIGHT_BLL m_Role_BLL = new SYSTEM_ROLE_RIGHT_BLL();
            SYSTEM_ROLE_RIGHT m_Role = new SYSTEM_ROLE_RIGHT();

            if (string.IsNullOrEmpty(rbtnRole.SelectedValue))
            {
                gfJs.SetAlertMsg(this.Page,"请选择角色!!!");
                return;
            }

            strRole = rbtnRole.SelectedValue;
            strRights = Request["rn"];

            if (string.IsNullOrEmpty(strRights))
            {
                gfJs.SetAlertMsg(this.Page, "用户权限不能设置为空!!!");
                return;
            }

            
            m_Role.role = strRole;
            m_Role.rights = ","+strRights+",";
            //修改该记录
            if (m_Role_BLL.GetRecordCount("role='"+strRole+"'")>0)
            {
                int oldId=int.Parse(DbHelper.GetValue("select id from SYSTEM_ROLE_RIGHT where role='"+strRole+"'"));
                m_df.Add(m_Role_BLL.UpdateSQL(m_Role, oldId));

                if (m_df.Execute() == true)
                {
                    gfJs.SetAlertMsg(this.Page, "修改权限成功!");
                }
                else
                {
                    gfJs.SetAlertMsg(this.Page, "权限修改失败!");
                }
            }
            //添加纪录
            else
            {
                m_Role.id = m_Role_BLL.GetMax_id();
                m_df.Add(m_Role_BLL.InsertSQL(m_Role));

                if (m_df.Execute() == true)
                {
                    gfJs.SetAlertMsg(this.Page, "保存权限成功!");
                }
                else
                {
                    gfJs.SetAlertMsg(this.Page, "权限添加失败!");
                }
            }

            string role = rbtnRole.SelectedValue;

            str_select_value = DbHelper.GetValue("select rights from SYSTEM_ROLE_RIGHT where role='" + role + "'");

            g_strTreeJS = BuildTree(str_select_value);

        }

        protected void rbtnRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            string role=rbtnRole.SelectedValue;

            str_select_value = DbHelper.GetValue("select rights from SYSTEM_ROLE_RIGHT where role='" + role + "'");

            g_strTreeJS = BuildTree(str_select_value);

        }

        
    }
}